package com.atguigu.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.auth.feign.MemberFeignService;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberRsepVo;
import com.atguigu.auth.vo.SocialUser;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/oauth2.0")
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    //https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&
    // grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
    @GetMapping("/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //0.使用微博返回的code码换取access-token
        Map<String, String> map = new HashMap<>();
        map.put("client_id", "974927266");
        map.put("client_secret", "64974f4bb0f9dc72790fa6f35e31087b");
        map.put("redirect_uri", "http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("grant_type", "authorization_code");
        map.put("code", code);
        HttpResponse weiboResponse = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), null, map);

        //1. 解析Access_Token
        if (weiboResponse.getStatusLine().getStatusCode() == 200) {
// 获取到了 Access Token
            String json = EntityUtils.toString(weiboResponse.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

            // 相当于我们知道了当前是那个用户
            // 1.如果用户是第一次进来 自动注册进来(为当前社交用户生成一个会员信息 以后这个账户就会关联这个账号)
            R login = memberFeignService.oAuthLogin(socialUser);
            if(login.getCode() == 0){
                MemberRsepVo rsepVo = login.getData("data" ,new TypeReference<MemberRsepVo>() {});
//
                log.info("\n欢迎 [" + rsepVo.getUsername() + "] 使用社交账号登录");
                // 登录成功 跳回首页
                session.setAttribute(AuthServerConstant.LOGIN_USER, rsepVo);
                return "redirect:http://gulimall.com";
            }else{
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }

        return "redirect:http://gulimall.com";
    }

    @GetMapping("/logout")
    public String login(HttpSession session){
        if(session.getAttribute(AuthServerConstant.LOGIN_USER) != null){
            log.info("\n[" + ((MemberRsepVo)session.getAttribute(AuthServerConstant.LOGIN_USER)).getUsername() + "] 已下线");
        }
        session.invalidate();
        return "redirect:http://auth.gulimall.com/login.html";
    }

}
